package com.scs.application.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.msg.entity.InnerNotice;
import com.scs.application.modules.msg.service.InnerNoticeService;
import com.scs.application.modules.sys.entity.Workbench;
import com.scs.application.modules.sys.mapper.WorkbenchMapper;
import com.scs.application.modules.sys.service.WorkbenchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 工作台 服务实现类
 * </p>
 */
@Service
public class WorkbenchServiceImpl extends BaseServiceImpl<WorkbenchMapper, Workbench> implements WorkbenchService {

    @Autowired
    private InnerNoticeService innerNoticeService;

    @Override
    public List<Workbench> listBySortNum(String deptId) {
        return this.baseMapper.listBySortNum(deptId);
    }

    @Override
    public List<InnerNotice> listNews() {
        List<InnerNotice> innerNoticeList = innerNoticeService.listNews();
        return innerNoticeList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchMenu(List<Workbench> entityList) {
        this.remove(Wrappers.<Workbench>query().eq("user_code", UserUtils.currentUser().getCode()).eq("flag_type", "menu"));
        if (entityList != null && !entityList.isEmpty()) {
            entityList.forEach(item -> {
                item.setDeptId(UserUtils.currentUser().getDeptId()).setDeptName(UserUtils.currentUser().getDeptName()).setUserCode(UserUtils.currentUser().getCode());
            });
            this.saveBatch(entityList);
        }
        return true;
    }
}
